from jenkins import Jenkins
import json

import pandas as pd


# jenkins批量发版
# pip install jenkinsapi
# https://blog.csdn.net/gitblog_00891/article/details/141879428
# 源码 https://github.com/pycontribs/jenkinsapi
# 官方文档 https://pycontribs.github.io/jenkinsapi/
def test():
    job_name = 'ifarm_cloudpc_service_be_testing'
    job_info = server.get_job_info(job_name)
    last_build_info = server.get_job_info(job_name)['lastCompletedBuild']
    print(f"Job '{job_name}' last build number: {last_build_info['number']}")

    # 构建
    parameters = {}
    parameters['ProjectName'] = 'cloudpc-job'
    parameters['branch'] = 'origin/testing2'
    print(parameters)
    number = server.build_job(job_name, parameters)
    print(f"'{job_name}' 创建构建成功 编号 '{number}' 参数 '{parameters}'")


if __name__ == '__main__':
    file_path = 'C:/Users/asyf/Desktop/jobs.xlsx'
    # 使用read_excel函数读取Excel文件并将其存储为DataFrame对象
    df = pd.read_excel(file_path, converters={'分公司代码': str}, sheet_name='Sheet1')

    JENKINS_URL = 'https://deploy.cpcti.com/'
    JENKINS_USERNAME = 'Deploy'
    JENKINS_API_TOKEN = 'qUV%oP090Q#ecu'

    server = Jenkins(JENKINS_URL, username=JENKINS_USERNAME, password=JENKINS_API_TOKEN)
    print(server.server)

    # test()
    for index, row in df.iterrows():
        job_name = row['job_name'].strip()
        parameters = eval(row['parameters'].strip())
        # print(type(job_name))
        # print(type(parameters))
        number = server.build_job(job_name, parameters)
        print(f"'{job_name}' 创建构建成功 编号 '{number}' 参数 '{parameters}'")
